<template>
 <van-icon
    color="#777"
    :name="value === 1 ? 'good-job' : 'good-job-o'"
    :class="{collcet: value === 1}"
    @click="onLike"
    :loading="loading"
  />
</template>

<script>
// 导入接口
import { addLike, deleteLike } from '@/api/article'
export default {
  data () {
    return {
      loading: false
    }
  },
  props: {
    value: {
      type: [Number, Boolean],
      required: true
    },
    artID: {
      type: [String, Number, Object],
      required: true
    }
  },
  methods: {
    async onLike () {
      let status = -1
      this.loading = true
      try {
        if (this.value === 1) {
          // 取消点赞
          await deleteLike(this.artID)
          status = -1
        } else {
          // 点赞
          await addLike(this.artID)
          status = 1
        }
        this.$emit('input', status)
        this.$toast.success(status === 1 ? '点赞成功' : '取消成功')
      } catch (err) {
        this.$toast('操作失败请重试')
      }
      this.loading = false
    }
  }
}
</script>

<style>
.collcet {
  color: springgreen!important;
}
</style>
